﻿/*!
 * 背景相关
 *
 *
 */

// 定义两个背景，前和后
var bgf = $('#background1').get(0).getContext('2d');
var bgb = $('#background2').get(0).getContext('2d');

// 前一个背景的alpha值
var backGroundAlpha = 1;

//图像对象


//方法
//测试用，闪屏用
bgf.test = function(){
	
	for (var i = 1; i<10; ++i){
		setTimeout(function(){
		bgf.fillStyle = '#333333';
		bgf.fillRect(0,0,800,600);},i*1000);
		setTimeout(function(){
		bgf.fillStyle = '#CCCCCC';
		bgf.fillRect(0,0,800,600);},i*1000+500);
	}
}
//画出一个背景
bgf.draw = function(src){
	bgf.clearRect(0, 0, 800, 600);
	var image = new Image();
	image.src = src;
	image.onload = function(){
		bgf.drawImage(image, 0, 0, 800, 600);
	}
}
//将bgf图像转移到bgb中
function fronttoback(){
	var image = bgf.getImageData(0, 0, 800, 600);
	bgb.putImageData(image, 0, 0);
	bgf.clearRect(0, 0, 800, 600);
}
bgf.putimage = function(img){
	bgf.clearRect(0, 0, 800, 600);
	// console.log(img);
	bgf.putImageData(img,0,0);
}
bgf.invert = function (){
	var image = bgf.getImageData(0, 0, 800, 600);
	
	for(var i=0,len = image.data.length;i<len;i+=4){
		var red = image.data[i],
			green = image.data[i+1],
			blue = image.data[i+2],
			alpha = image.data[i+3];
 
		image.data[i] = 255-red;
		image.data[i+1] = 255-green;
		image.data[i+2] = 255-blue;
		image.data[i+3] = alpha;
	}
	return image;
}


